<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Evan
 * Date: 12-10-11
 * Time: 下午1:35
 * To change this template use File | Settings | File Templates.
 */
class Model_Cfg_Menus extends Jwawa_BaseModel implements Jwawa_Tree_Node
{
    public $id;
    public $hierarchy_flag;
    public $seq_nbr;
    public $children;
    public $page_code;
    public $icon;

    function __construct($prop_array = null)
    {
        parent::__construct($prop_array);
        $this->children = array();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getHierarchyFlag()
    {
        return $this->hierarchy_flag;
    }

    public function getSeqNbr()
    {
        return $this->seq_nbr;
    }

    public function &getChildren()
    {
        return $this->children;
    }

    public function getIcon(){
        return $this->icon;
    }

    public static function getMenuById($id){
        if (!isset($id)){
            return null;
        }

        $sql = "SELECT cu.id,
                cu.menu_name,
                cu.short_name,
                cu.seq_nbr,
                cu.hierarchy_flag,
                cu.parent_id,
                cu.is_leaf,
                cps.page_code,
                cu.icon,
                ca.action_code
            FROM cfg_menus cu
            LEFT JOIN cfg_search_pages cps ON cps.id = cu.page_id
            JOIN cfg_actions ca on ca.id = cps.action_id
            WHERE cu.id = :id
              AND cu.is_deleted = 'N' ";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('id', $id);
        $stmt->execute();
        $rst = $stmt->fetchObj();

        return $rst;
    }

    private static function findMenuListByIds($menu_ids)
    {
        if (!isset($menu_ids) || count($menu_ids) == 0){
            return array();
        }

        $ids = implode(',', $menu_ids);
        $sql = "select cu.id,
                    cu.menu_name,
                    cu.short_name,
                    cu.seq_nbr,
                    cu.hierarchy_flag,
                    cu.parent_id,
                    cu.is_leaf,
                    cps.page_code,
                    cu.icon
            from cfg_menus cu
            left join cfg_search_pages cps on cps.id = cu.page_id
            where cu.id in ($ids)
              and cu.is_deleted = 'N' ";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
//        $stmt->bindValue('isDeleted', 'N');
        $stmt->execute();
        $rst = $stmt->fetchAllObj();

        return $rst;
    }

    private static function findUserMenuHierarchyFlagList($login_account_id)
    {
        $sql = "select cm.hierarchy_flag
            from plg_role_login_ships prls
            join plg_roles pr on pr.id = prls.plg_role_id
            join plg_role_action_ships pra on pra.plg_role_id = prls.plg_role_id
            join cfg_actions ca on ca.id = pra.plg_action_id
            join cfg_search_pages csp on csp.action_id = ca.id
            join cfg_menus cm on cm.page_id = csp.id
            where prls.sso_login_account_id = :login_account_id
              and pr.is_deleted = 'N'
              and ca.is_deleted = 'N'
              and cm.is_deleted = 'N' ";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('login_account_id', $login_account_id);
        $stmt->execute();
        $rst = $stmt->fetchAll(PDO::FETCH_COLUMN);

        return $rst;
    }

    private static function getMenuIdListInHierarchyFlagList($hierarchy_flag_list)
    {
        $menu_id_list = array();
        foreach ($hierarchy_flag_list as $hc) {
            $shca = explode('.', $hc, -1);
//            self::getLog()->debug($shca);
            foreach ($shca as $val) {
                $exist = false;
                for ($i = 0; $i < count($menu_id_list); $i++) {
                    if ($val == $menu_id_list[$i]) {
                        $exist = true;
                        break;
                    }
                }
                if (!$exist) {
                    array_push($menu_id_list, intval($val));
                }
            }
        }

        return $menu_id_list;
    }

    private static function convertMenuListToHash($menu_list){
        $menu_hash = array();
        foreach($menu_list as $menua){
            $menu = new Model_Cfg_Menus($menua);
            $menu_hash[$menu->id] = $menu;
        }

        return $menu_hash;
    }

    public static function findUserMenuTree($login_account_id)
    {
        $flag_list = self::findUserMenuHierarchyFlagList($login_account_id);
        $menu_id_list = self::getMenuIdListInHierarchyFlagList($flag_list);
        $menu_list = self::findMenuListByIds($menu_id_list);
        $menu_hash = self::convertMenuListToHash($menu_list);

        $root = new Model_Cfg_Menus();
        Jwawa_Tree_Builder::buidTree($root, $menu_hash);

        return $root;
    }
}

